package com.shenmazong.appdemodocx.utils;





import cn.hutool.core.io.FileUtil;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Xml2DocxUtil {
    /**
     *
     * @param outFile 输出文件
     * @param dataMap 数据
     * @param document 模板文件
     * @param documentXmlRels 模板关联文件
     * @param originalTemplate 模板原始docx文件
     * @return retMap succ 是否成功 true & false ; msg返回异常信息
     */
    public static Map<String,Object> Xml2Docx(File outFile, Map<String,Object> dataMap, String document, String documentXmlRels, String originalTemplate){
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("succ",true);
        ZipOutputStream zipout = null;
        OutputStream outputStream = FileUtil.getOutputStream(outFile);
        try {
            //图片配置文件模板
            ByteArrayInputStream documentXmlRelsInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, documentXmlRels);
            //内容模板
            ByteArrayInputStream documentInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, document);
            ZipFile zipFile = new ZipFile(originalTemplate);
            Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
            zipout = new ZipOutputStream(outputStream);
            //开始覆盖文档------------------
            int len = -1;
            byte[] buffer = new byte[1024];
            while (zipEntrys.hasMoreElements()) {
                ZipEntry next = zipEntrys.nextElement();
                InputStream is = zipFile.getInputStream(next);
                if (next.toString().indexOf("media") < 0) {
                    zipout.putNextEntry(new ZipEntry(next.getName()));
                    if (next.getName().indexOf("document.xml.rels") > 0) { //如果是document.xml.rels由我们输入
                        if (documentXmlRelsInput != null) {
                            while ((len = documentXmlRelsInput.read(buffer)) != -1) {
                                zipout.write(buffer, 0, len);
                            }
                            documentXmlRelsInput.close();
                        }
                    } else if ("word/document.xml".equals(next.getName())) {//如果是word/document.xml由我们输入
                        if (documentInput != null) {
                            while ((len = documentInput.read(buffer)) != -1) {
                                zipout.write(buffer, 0, len);
                            }
                            documentInput.close();
                        }
                    } else {
                        while ((len = is.read(buffer)) != -1) {
                            zipout.write(buffer, 0, len);
                        }
                        is.close();
                    }
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
            retMap.put("succ",false);
            retMap.put("msg",e.getMessage());
        }
        finally {
            if(zipout!=null){
                try {
                    zipout.close();
                } catch (IOException e) {
                    e.getStackTrace();
                    retMap.put("succ",false);
                    retMap.put("msg",e.getMessage());
                }
            }
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.getStackTrace();
                    retMap.put("succ",false);
                    retMap.put("msg",e.getMessage());
                }
            }
        }
        return retMap;
    }
}
